use std::{
    path,
    fs
};

pub struct Files {
    paths: Vec<String>,
    selected: usize,
}
impl Default for Files {
    fn default() -> Self {
        Self{ 
            paths: fs::read_dir(".").unwrap().map(|a|{
                a.unwrap().path().to_str().unwrap().to_string()
            }).collect(),
            selected: 0
        }
    }
}

impl Files {
    pub fn new(path:Option<&str>) -> Self {
        if let Some(path) = path{
            Files{
                paths: fs::read_dir(path).unwrap().map(|a|{
                    a.unwrap().path().to_str().unwrap().to_string()
                }).collect(),
                selected:0
            }
        }else{
            Files::default()
        }
    }

    pub fn selected_path(&self) -> &str {
        &self.paths[self.selected]
    }

    pub fn next(&mut self) {
        self.selected = if self.selected == 2 {
            0
        } else {
            self.selected + 1
        }
    }

    pub fn previous(&mut self) {
        self.selected = if self.selected == 0 {
            2
        } else {
            self.selected - 1
        }
    }
}